<template>
  <div class="article-container" ref="fathor">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->
    <!-- 加载中 loading -->
    <van-loading class="article-loading" v-if="showLoading" />
    <!-- /加载中 loading -->
    <!-- 404部分 文章没有获取到的时候显示 -->
    <div class="error404" v-if="isCorrect">
      <img src="./img/404.jpg" alt="" />
    </div>
    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ curResult.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          :src="curResult.aut_photo"
          fit="fill"
        />
        <div class="text">
          <p class="name">{{ curResult.aut_name }}</p>
          <p class="time">{{ curResult.pubdate | rTime }}</p>
        </div>
        <van-button round size="small" type="info" @click="isFollow">{{
          is_followed ? "已关注" : "+ 关注"
        }}</van-button>
      </div>
      <div class="content">
        <!-- 因为后台返回的内容有的是包含标签和文本，所以用v-html渲染 -->
        <div v-html="curResult.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          icon="good-job-o"
          @click="isLike"
          >{{ curResult.attitude === 1 ? "取消点赞" : "点赞" }}</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="isLove"
          >{{ curResult.attitude === 0 ? "取消不喜欢" : "不喜欢" }}</van-button
        >
      </div>
      <!-- 评论组件 -->
      <Comment
        :articleID="art_id.toString()"
        @editScroll="editScroll"
      ></Comment>
    </div>
  </div>
</template>
<script>
// 引入获取指定文章的详情接口,关注作者和取消关注接口
import {
  getArticleDetail,
  followAuthor,
  disFollowAuthor,
  giveLike,
  disGiveLike,
  dislikeArticle,
  cancelDislike
} from "@/api/article.js";
// 引入评论组件
import Comment from "./comment.vue";
export default {
  data() {
    return {
      art_id: null, //当前文章id
      showLoading: true, //加载的显示与隐藏
      isCorrect: false, //404状态
      curResult: {}, //当前文章的详情
      is_followed: null //当前文章作者关注状态
    };
  },
  components: {
    Comment
  },
  created() {
    //注意这里不能换成mounted,因为在beforeMount之后mounted之前，就开始创建子组件了，
    // 但是这是文章id还没有传给子组件，子组件就拿不到，获取数据就会出错
    this.art_id = this.$route.params.id;
    // 获取文章详情
    this.hgetArticle();
  },
  methods: {
    // 获取文章的详细信息
    async hgetArticle() {
      try {
        const res = await getArticleDetail(this.art_id);
        console.log(res);
        this.curResult = res.data.data;
        this.is_followed = this.curResult.is_followed;
        // this.art_id = this.curResult.art_id;
        //加载完成
        this.showLoading = false;
      } catch (err) {
        console.log(err);
        this.showLoading = false;
        // 文章没有获取显示404
        this.isCorrect = true;
      }
    },
    // 是否关注文章作者
    async isFollow() {
      const follow = this.is_followed;
      const aut_id = this.curResult.aut_id.toString();
      // console.log(typeof aut_id);
      try {
        if (follow) {
          // 已经关注了
          // 调接口取消关注
          await disFollowAuthor(aut_id);
          // 把本地的is_followed改为false
          this.is_followed = false;
        } else {
          // 还没有关注
          // 调接口关注
          await followAuthor(aut_id);
          // 更改is_followed为true
          this.is_followed = true;
        }
      } catch (err) {
        console.log(err);
        this.$toast.fail("关注失败");
      }
    },
    //点赞和取消点赞
    async isLike() {
      const attitude = this.curResult.attitude;
      const art_id = this.art_id.toString();
      try {
        if (attitude === 1) {
          // 已经点赞了，取消点赞
          // 调接口取消点赞
          await disGiveLike(art_id);
          // 修改本地attitude为-1
          this.curResult.attitude = -1;
        } else {
          // 没有点赞,调接口点赞
          await giveLike(art_id);
          // 修改本地的attitude为1
          this.curResult.attitude = 1;
        }
      } catch (err) {
        console.log(err);
        this.$toast.fail("操作失败");
      }
    },
    // 喜欢和不喜欢
    async isLove() {
      let art_id = this.art_id.toString();
      let attitude = this.curResult.attitude;
      try {
        if (attitude === 0) {
          // 已经不喜欢
          // 调接口取消不喜欢
          await cancelDislike(art_id);
          // 修改attitude为-1
          this.curResult.attitude = -1;
        } else {
          // 取消不喜欢状态
          // 调接口改为不喜欢
          await dislikeArticle(art_id);
          // 修改attitude为0
          this.curResult.attitude = 0;
        }
      } catch (err) {
        console.log(err);
        this.$toast.fail("是否不喜欢操作失败");
      }
    },
    //修改父组件的scroll
    editScroll() {
      this.$refs.fathor.scrollTop = this.$refs.fathor.scrollHeight;
    }
  }
};
</script>
<style lang="less" scoped>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
  padding: 10px 0 40px 0;
}
.article-loading {
  padding-top: 10px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.error404 {
  width: 100%;
  padding-top: 20px;
  img {
    width: 100%;
  }
}
.detail {
  padding: 50px 10px 0 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
